package com.ruoyi.system.service.impl;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.Random;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.Mail.MailUtils;
import com.ruoyi.system.domain.*;
import com.ruoyi.system.dto.ReceiptOrderDTO;
import com.ruoyi.system.mapper.*;
import com.ruoyi.system.vo.ReceiptOrderVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.service.IReceiptOrderService;

/**
 * 入库单Service业务层处理
 *
 * @author ruoyi
 * @date 2024-12-12
 */
@Service
public class ReceiptOrderServiceImpl implements IReceiptOrderService {
    @Autowired
    private ReceiptOrderMapper receiptOrderMapper;
    @Autowired
    private ReceiptOrderDetailMapper receiptOrderDetailMapper;
    @Autowired
    private HistoryMapper historyMapper;
    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private SupplierTransactionMapper supplierTransactionMapper;
    @Autowired
    private InventoryMapper inventoryMapper;

    /**
     * 查询入库单
     *
     * @param id 入库单主键
     * @return 入库单
     */
    @Override
    public ReceiptOrder selectReceiptOrderById(Long id) {
        return receiptOrderMapper.selectReceiptOrderById(id);
    }

    /**
     * 查询入库单列表
     *
     * @param receiptOrder 入库单
     * @return 入库单
     */
    @Override
    public List<ReceiptOrderVo> selectReceiptOrderList(ReceiptOrder receiptOrder) {
        return receiptOrderMapper.selectReceiptOrderList(receiptOrder);
    }

    /**
     * 新增入库单
     *
     * @param receiptOrder 入库单
     * @return 结果
     */
    @Override
    public int insertReceiptOrder(ReceiptOrder receiptOrder) {
        receiptOrder.setCreateTime(DateUtils.getNowDate());
        Supplier supplier = supplierMapper.selectSupplierById(receiptOrder.getSupplierId());
        MailUtils.register(supplier.getSupplierName(), receiptOrder.getOrderNo(), receiptOrder.getPayableAmount(), supplier.getEmail());
        return receiptOrderMapper.insertReceiptOrder(receiptOrder);
    }

    /**
     * 修改入库单
     *
     * @param receiptOrder 入库单
     * @return 结果
     */
    @Override
    public int updateReceiptOrder(ReceiptOrder receiptOrder) {
        receiptOrder.setUpdateTime(DateUtils.getNowDate());
        return receiptOrderMapper.updateReceiptOrder(receiptOrder);
    }


    /**
     * 入库
     *
     * @param receiptOrder 入库单
     * @return 结果
     */
    @Override
    public int ReceiptOrder(ReceiptOrderDTO receiptOrder) {
        List<ReceiptOrderDetail> details = receiptOrder.getDetails();
        //在途
        if (receiptOrder.getReceiptOrderStatus() == 3) {
            receiptOrderMapper.ReceiptOrder(receiptOrder);
            for (ReceiptOrderDetail detail : details) {
                detail.setReceiptOrderStatus(3L);
                receiptOrderDetailMapper.updateDetail(detail);
            }
        }
        if (receiptOrder.getReceiptOrderStatus() == 5 || receiptOrder.getReceiptOrderStatus() == 4) {
            //修改入库单状态
            List<ReceiptOrderDetail> list = receiptOrderDetailMapper.selectReceiptOrderDetail(Long.valueOf(receiptOrder.getReceiptOrderNo()));
            if (list.size() != receiptOrder.getDetails().size()) {
                receiptOrder.setReceiptOrderStatus(4L);
                receiptOrderMapper.ReceiptOrder(receiptOrder);
                for (ReceiptOrderDetail detail : details) {
                    detail.setReceiptOrderStatus(5L);
                    receiptOrderDetailMapper.updateDetail(detail);
                }
            } else {
                receiptOrderMapper.ReceiptOrder(receiptOrder);
                for (ReceiptOrderDetail detail : details) {
                    detail.setReceiptOrderStatus(5L);
                    receiptOrderDetailMapper.updateDetail(detail);
                }
            }
            //添加入库记录
            History history = new History();
            Inventory inventory = new Inventory();
            List<ReceiptOrderDetail> detail1 = receiptOrderDetailMapper.selectReceiptOrderDetailByStatus(Long.valueOf(receiptOrder.getReceiptOrderNo()));
            //判断物料是否全部入库
            if (list.size() == detail1.size()) {
                receiptOrder.setReceiptOrderStatus(5L);
                receiptOrderMapper.ReceiptOrder(receiptOrder);
                //添加账户流水记录
                String transactionCodeId = "Ts_" + System.currentTimeMillis();
                BigDecimal bigDecimal = supplierTransactionMapper.selectSupplierById(String.valueOf(receiptOrder.getSupplierId()));
                SupplierTransaction su = new SupplierTransaction();
                su.setTransactionCode(transactionCodeId);
                su.setSupplierId(String.valueOf(receiptOrder.getSupplierId()));
                su.setTransactionType(String.valueOf(1));
                su.setTransactionAmount(receiptOrder.getPayableAmount());
                if (bigDecimal == null) {
                    su.setPreviousBalance(new BigDecimal(0));
                    su.setCurrentBalance(receiptOrder.getPayableAmount().add(new BigDecimal(0)));
                } else {
                    su.setPreviousBalance(bigDecimal);
                    su.setCurrentBalance(receiptOrder.getPayableAmount().add(bigDecimal));
                }
                su.setReceiptOrderId(Long.valueOf(receiptOrder.getReceiptOrderNo()));
                su.setRemark(receiptOrder.getRemark());
                su.setCreateTime(DateUtils.getNowDate());
                supplierTransactionMapper.insertSupplierTransaction(su);
                Supplier s=new Supplier();
                s.setPayableAmount(su.getCurrentBalance());
                s.setSupplierNo(String.valueOf(receiptOrder.getSupplierId()));
                supplierMapper.updateSupplier(s);
            }
            for (ReceiptOrderDetail detail : details) {
                history.setFormId(detail.getReceiptOrderId());
                history.setFormType(1L);
                history.setItemId(detail.getItemId());
                history.setQuantity(detail.getRealQuantity());
                history.setRemark(detail.getRemark());
                history.setCreateTime(DateUtils.getNowDate());
                history.setWarehouseId(detail.getWarehouseId());
                history.setAreaId(detail.getAreaId());
                historyMapper.insertHistory(history);
                //添加入库单详情中的物料集合至库存
                Inventory inventory1 = inventoryMapper.selectInventoryById(detail.getItemId(), detail.getWarehouseId(), detail.getAreaId());
                if (inventory1 != null) {
                    inventory.setId(inventory1.getId());
                    inventory.setUpdateTime(DateUtils.getNowDate());
                    inventory.setQuantity(inventory1.getQuantity().add(detail.getRealQuantity()));
                    inventoryMapper.updateInventory(inventory);
                } else {
                    inventory.setItemId(detail.getItemId());
                    inventory.setRemark(detail.getRemark());
                    inventory.setQuantity(detail.getRealQuantity());
                    inventory.setCreateTime(DateUtils.getNowDate());
                    inventory.setWarehouseId(detail.getWarehouseId());
                    inventory.setAreaId(detail.getAreaId());
                    inventoryMapper.insertInventory(inventory);
                }
            }
        }

        return 0;
    }

    /**
     * 批量删除入库单
     *
     * @param ids 需要删除的入库单主键
     * @return 结果
     */
    @Override
    public int deleteReceiptOrderByIds(Long[] ids) {
        receiptOrderDetailMapper.deleteReceiptOrderDetailByIds(ids);
        return receiptOrderMapper.deleteReceiptOrderByIds(ids);
    }

    /**
     * 删除入库单信息
     *
     * @param id 入库单主键
     * @return 结果
     */
    @Override
    public int deleteReceiptOrderById(Long id) {
        return receiptOrderMapper.deleteReceiptOrderById(id);
    }

    /**
     * 看板数据统计查询
     *
     * @param status 传入状态
     * @return 结果
     */
    @Override
    public int selectStatus(int status) {
        return receiptOrderMapper.selectStatus(status);
    }

    /**
     * 看板趋势统计查询
     *
     * @return 结果
     */
    @Override
    public List<Map<String, Object>> selectDate(String date1, String date2) {
        return receiptOrderMapper.selectDate(date1, date2);
    }
}
